//
//  CZNewfeatureViewController.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/19.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class CZNewfeatureViewController: UICollectionViewController,UIApplicationDelegate {

    private let layout = UICollectionViewFlowLayout()
    //创建一个有layout参数的控制器
    init() {
    
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //页面的个数
    private let pageCount = 4
    override func viewDidLoad() {
        super.viewDidLoad()

        
        prepareCollectionView()
    
    }
    
    func prepareCollectionView() {
    
        //注册
        self.collectionView!.registerClass(CZNewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置布局参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        //不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //分页显示
        collectionView?.pagingEnabled = true
        
        //取消弹簧效果
        collectionView?.bounces = false
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return pageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewfeatureCell
        
        cell.index = indexPath.item
    
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        
        
        //获取当前显示的cell的indexpath
      let visibleIndexpath = collectionView.indexPathsForVisibleItems().last!
                
        if visibleIndexpath.item == pageCount-1 {
        
            //获得正在显示的cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexpath) as! CZNewfeatureCell
            
            //调用cell的动画
            
            cell.startButtonAnimation()
            
        }
    }
}




// MARK: - 自定义cell显示新特性界面

class CZNewfeatureCell: UICollectionViewCell {
    
    
    //属性 index 当index改变，就用index的值得图片今年偏号
    
    var index = 0 {
        
        didSet{
        
            bgImage.image = UIImage(named: "new_feature_\(index+1)")
            
            //隐藏开始按钮
            starButton.hidden = true
        }
    
    }
    
    //设置子控件的frame
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        prepareUI()
    }
    
    //准备UI
    func prepareUI(){
    
        //添加背景图片和按钮
        contentView.addSubview(bgImage)
        contentView.addSubview(starButton)
        
//        bgImage.translatesAutoresizingMaskIntoConstraints = false
//        starButton.translatesAutoresizingMaskIntoConstraints = false
        
        //设置约束
        
        bgImage.ff_Fill(contentView)
        
//       contentView.addConstraint(NSLayoutConstraint(item: bgImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        
//        contentView.addConstraint(NSLayoutConstraint(item: bgImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//    
//        contentView.addConstraint(NSLayoutConstraint(item: bgImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
//        
//        contentView.addConstraint(NSLayoutConstraint(item: bgImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //设置开始按钮的约束
        
        starButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
        
//        contentView.addConstraint(NSLayoutConstraint(item: starButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        
//        contentView.addConstraint(NSLayoutConstraint(item: starButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
    }
    
    
    //开始按钮的动画
    func startButtonAnimation() {
    
        starButton.hidden = false
        
        //设置开始按钮scale
        starButton.transform = CGAffineTransformMakeScale(0, 0)
        
        //使用UIVie动画
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
                self.starButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("开始按钮动画完成")
        }
    
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //点击开始按钮是调用该方法
    func startButtonClick() {
    
        //获取Appdelegate代理
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
    }
    
    
    
    //MARK: - 懒加载
    
    ///背景图片
    private lazy var bgImage = UIImageView(image: UIImage(named: "new_feature_1"))
    
    
    ///开始按钮
    private lazy var starButton: UIButton = {
    
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)

        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        //隐藏开始按钮
        button.hidden = true
        
        //点击开始按钮调用方法
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
      
      return button
    }()
    
}

